//启动文件 
const App = getApp();
let that;
Page({
  name: '启动页面',
  data: {
    // userInfo 用户信息
    userInfo: {

    },
    siteInfo: App.siteInfo,
  },
  init: function (call) {
    console.log("%c \n\n-------------------- api   ----------------", "color:red", '域名', this.data.siteInfo.api);
    console.log("%c \n\n-------------------- project_id   ----------------", "color:red", 'project_id', this.data.siteInfo.project_id);

    wx.login({
      //成功返回
      success: (res) => {
        let code = res.code;
        let that = this;
        //    参数 code   调用接口   获取oppenid
        //  获取   oppenid  
        const url = '/open/wx_app_api/login';
        App.test().post(url, {
          code
        }, false, {
          header: '',
          //  配置 请求头 之类的, 之类的一些数据 
        }, true, true).then(function (r) {
          if (r.code == 200) {
            wx.setStorageSync('Account', r.data)
          }
        })
      }
    })
  },
  onLoad: function (options) {
    if (options.scene) {
      //朋友圈-海报分享进入
      let scene = options.scene;
      let arr = scene.split('-');
      if (parseInt(arr[0]) == 1) {
        //商品详情
        App.globalData.goods_id = arr[1];
      }
    } else {
      if (options.type == 1) {
        App.globalData.goods_id = options.goods_id;
      }else if(options.type == 2){
        App.globalData.order_number = options.order_number;
      }
    }
    if (App.globalData.isConnected) {
      let getTabbar = async () => {
        try {
          //  登录 
          this.init(function (res) {
            if (res?.data?.code == 200) {
              wx.setStorage({
                data: res.data.data,
                key: 'Account',
                success: function () {
                  App.systemStore('Account', 'a')
                }
              })
              //  判断是否有手机号 
              //  全局存储 登录状态
              if (wx.getStorageSync('Account').phone) {
                App.globalData.accountStatus = 1;
              }
            }
          });
          //  
          // 存储成功 
          //   用户信息缓存 
          //  用户是否登录 
          //  用户首次加载 
          const tabs = await App.getBase();
          // console.log("%c \n\n-------------------- tabbar   ----------------", "color:green", tabs);
          //  赋值 拼接  tabbar数组路径
          let cutom_arr = ["首页", "分类", "购物车", "我的"]
          if (tabs.data.tabbar_items) {
            let _tabbar_item = (tabar, keys) => {
              let tabbar_items = [];
              let obj = {};
              let obj1 = {};
              tabar.map((item, index) => {
                item.name = cutom_arr[index];
                for (let key in item) {
                  if (Array.isArray(item[key])) {
                    //  数组解构 
                    const [x] = item[key]
                    //  进行合并 
                    obj[key] = x
                  } else {
                    obj1[key] = item[key]
                  }
                }
                tabbar_items.push(Object.assign({}, obj, obj1))
              })
              console.log(App.globalData.tabbar_items)
              App.globalData.tabbar_items = tabs.data.tabbar_items;
              App.globalData.tabbar_config = tabs.data.tabbar_config
            }
            tabs.data.tabbar_items.length && _tabbar_item(tabs.data.tabbar_items)
          }
        } catch (error) {
          //  关闭弹窗
          wx.showToast({
            title: 'error',
            icon: 'none'
          })
        }
        setTimeout(() => {
          //  启动页  跳转main 页面 
          wx.switchTab({
            url: '/pages/nav/01/index',
          })
        }, 500)
      }
      getTabbar()
    }
    // 无网络状态重新 进网络状态
    if (!App.globalData.isConnected) {
      wx.reLaunch({
        url: '/pages/wait/connect/index',
      })
    }
  },
})